//对添加和修改的js进行封装
const util = {
    //关闭弹出表单窗口
    closeLayer: ($, res) => {
        if (res.code == 0) {
            layer.msg(res.msg + "<br>将在3秒后自动关闭", {time: 3000});
            setTimeout(() => {
                let index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                parent.layer.close(index); //再执行关闭
                window.parent.location.reload();//新增成功后刷新父界面
            }, 3000)
        } else layer.msg(res.msg);
    },
    //crud操作请求访问
    crudAjax: ($, url, data) => {
        $.ajax({
            type: 'post',
            url: url,
            dataType: 'json',
            data: data.field,
            success: function (res) {
                util.closeLayer($, res);
            }
        })
    },
    //查询代码是否重复
    clashAjax: ($, url, code) => {
        $.ajax({
            type: "post",
            url: url + '?time' + new Date().getTime(),
            dataType: "json",
            data: {code},
            success: (res) => {
                if (res.code == 0) {
                    return 1;
                } else {
                    layer.msg(res.msg);
                    return 0;
                }
            }
        })
    },
    //添加下拉框
    addSelect: ($, res, str, value) => {
        if (res.code == 0) {
            let data = eval(res.data);
            if (str == 'major') {
                $("#" + str).empty(); // 清空下拉框内容
            }
            for (let i = 0; i < data.length; i++) {
                if (str == 'classTutor') {
                    $("#" + str).append("<option value=\"" + data[i].id + "\">" + data[i].realName + "</option>");
                } else {
                    $("#" + str).append("<option value=\"" + data[i].id + "\">" + data[i].name + "</option>");
                }
            }
            if (value != null) {
                $("#" + str).val(value);
            }
            //重新渲染
            layui.form.render("select");
        } else {
            layer.msg(res.msg);
        }
    },
    //动态下拉框 查询选项
    getSelectAjax: ($, url, str, value, id) => {
        id = id ? id : 0;
        $.ajax({
            type: 'post',
            url: url,
            dataType: 'json',
            data: {id: parseInt(id)},
            success: function (res) {
                util.addSelect($, res, str, value);
            }
        });
    },
    //发送手机短信验证码
    mobileCode: ($, phone) =>{
        if (!(/^1(3|4|5|6|7|8|9)\d{9}$/.test(phone))) {
            layer.msg("手机号码有误，请重填");
        } else {
            $.ajax({
                type: "post",
                url: "/Class/login/mobileCode",
                dataType: "json",
                data: { "phone": phone},
                success: (res) => {
                    if (res.code == 0) {
                        layer.msg(res.msg)
                    } else layer.msg(res.msg)
                }
            })
        }
    },
    //发送邮箱验证码
    emailCode:($, email) =>{
        let arrs = new Array();
        if (!(/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/.test(email))) {
            layer.msg("邮箱格式有误，请重填");
        } else {
            arrs[0] = email;
            $.ajax({
                type: "post",
                url: "/Class/user/emailCode",
                dataType: "json",
                data: JSON.stringify(arrs),
                contentType : 'application/json',
                success: (res) => {
                    if (res.code == 0) {
                        layer.msg(res.msg)
                    } else layer.msg(res.msg)
                }
            })
        }
    },
    //多个邮箱发送
    manyEmailInfo: ($, email) =>{
        $.ajax({
            type: "post",
            url: "/Class/user/emailCode",
            dataType: "json",
            data: { "email": JSON.stringify(email)},
            contentType : 'application/json',
            success: (res) => {
                if (res.code == 0) {
                    layer.msg(res.msg)
                } else layer.msg(res.msg)
            }
        })
    },
    //转化时间格式
    getTime: (time) =>{
        let date = new Date(time);
        let y = date.getFullYear();
        let m = date.getMonth() + 1;
        let d = date.getDate();
        return `${y}年-${m}月-${d}日`;
    }
}

